package com.ulook.lookplanet.ui.matching;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.base.BaseFragment;
import com.ulook.lookplanet.main.MainActivity;
import com.ulook.lookplanet.ui.matching.heart.HeartActivity;
import com.ulook.lookplanet.widget.MatchingCardView;

import java.util.List;

public class MatchingFragment extends BaseFragment {

    private MatchingViewModel viewModel;
    private View root;
    private LinearLayout cardLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        viewModel = new ViewModelProvider(this).get(MatchingViewModel.class);
        root = inflater.inflate(R.layout.fragment_matching, container, false);

        viewModel.getToastText().observe(getActivity(), s -> {
            Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
        });
        viewModel.getCards().observe(getActivity(), vMatchingCards -> {
            displayCard(vMatchingCards);
        });

        viewModel.setCurrUserId(((MainActivity) getActivity()).getCurrUser().getId());

        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initTopBar();
        initData();
    }

    private void initTopBar(){
        QMUITopBarLayout topBar = root.findViewById(R.id.matching_bar);
        topBar.setTitle(R.string.title_matching);

        // TODO 换一批卡片
        /*topBar.addLeftImageButton(R.drawable.ic_matching_refresh, 1).setOnClickListener(view -> {

        });*/

        topBar.addRightImageButton(R.drawable.ic_heart_test, 2).setOnClickListener(view -> {
            // 心灵测式
            if(viewModel.getCurrUserId() != 0){
                Intent intent = new Intent(getActivity(), HeartActivity.class);
                intent.putExtra("currUserId", viewModel.getCurrUserId());
                startActivity(intent);
            }else {
                Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData(){
        cardLayout = root.findViewById(R.id.matching_card_layout);
        viewModel.getRecommendUser();
    }

    /**
     * 展示卡片
     */
    private void displayCard(List<VMatchingCard> cards){
        for(int i = 0; i < cards.size(); i++){
            Log.d("#####", cards.get(i).toString());
            MatchingCardView cardView = new MatchingCardView(getActivity());
            cardView.setWidgetData(cards.get(i), viewModel.getCurrUserId());
            cardLayout.addView(cardView);
        }
    }
}
